﻿<!DOCTYPE html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="<%=basePath%>web/js/jquery.min.js"></script>
    <script src="<%=basePath%>web/js/jquery.easyui.min.js"></script>
    <script src="<%=basePath%>web/js/easyui-lang-zh_CN.js"></script>
    <link rel="stylesheet" href="<%=basePath%>web/themes/icon.css">
    <link rel="stylesheet" href="<%=basePath%>web/themes/default/easyui.css">
    <link rel="stylesheet" href="<%=basePath%>web/css/wu.css">
    <script>
        var  selectIndex = -1;

        function hideTable(){
            $("#searchdiv").toggle();
        }

          
        $(function(){
          
            $("#productList").datagrid({
                url:'/kxmanage/sellorderbill/findall',
                pageNumber:1,
                pageSize:5,
                pageList:[5,10],
                pagination:true,
                checkOnSelect:true,
                columns:[[
                    {field:'ck',checkbox:true},
                    {field:'code',title:'订单编号',width:150,
                       formatter: function(value,row,index){
      	                 return '<a onclick="showSalesOrders(\'' + row.code+ '\')" style="color:blue;text-decoration:underline"  href="#">'+row.code+'</a>';
                       }  	
                    },
                    {field:'orderTime',title:'订单日期',width:100,
                        formatter:function(value,row,index){
                            return new Date(value).toLocaleDateString().replace(/\//g,'-');
                    	}
                    },
                    {field:'customName',title:'客户名称',width:180},
                    {field:'money',title:'总货值',width:100,
                        formatter:function(value,row,index){
                            return "￥"+row.money;
                        }
                    },
                    {field:'linkman',title:'联系人',width:100},
                    {field:'phone',title:'联系方式',width:100},
                    {field:'checkStatusName',title:'审核状态',width:95,
                      styler:function(value,row,index){
                        if(row.checkStatus==1){
                          return "color:red";
                        }
                      }
                    },
                    {field:'payStatusName',title:'支付状态',width:95},
                    {field:'name6',title:'操作员',width:100,
                    formatter:function (value,row,index) {
                     var str="${user.userName}";
                     return str;
                    }
                    },
                    {field:'opt',title:'操作',width:100,formatter:function (value,row,index) {
                            var context="<a href='#' onclick=\"updateSalesOrders('"+row.code+"')\" style='color:blue;text-decoration:underline'>修改</a>&nbsp;&nbsp;"+
                                "<a href='#' onclick=\"delSalesOrders('"+row.code+"','"+row.checkStatus+"')\" style='color:blue;text-decoration:underline'>删除</a>&nbsp;&nbsp;" 
                            return context;
                        }}

                ]],
                toolbar:'#wu_toolbar', 
                onClickRow:function(index,data){
                  $(this).datagrid('unselectRow',index);
                },
                pagination:true ,//向后台传递参数page 和rows 
                onDblClickRow : function(index, row) {
	                $("#show").show();
	                $("#detailed").html("<h3>单据标号为：<span id='oldCode'>"+row.code+"</span> 的明细如下所列</h3>");
	                $("#partList").datagrid({
	                url:'/kxmanage/sellorderbill/findpartsbysellcode',
	                queryParams:{sellcode:row.code},
	                checkOnSelect:true,
	                rownumbers:true,
	                columns:[[
	                    {field:'code',title:'配件编号',width:130},
	                    {field:'partName',title:'配件名称',width:190},
	                    {field:'partBrand',title:'配件品牌',width:120},
	                    {field:'partNo',title:'配件型号',width:100},
	                    {field:'amount',title:'数量',width:100},
	                    {field:'sellPrice',title:'单价',width:100, 
	                        formatter:function(value,row,index){
	                         if(row.sellPrice==null){
	                    		return null;
	                    	 }
	                            return "￥"+row.sellPrice;
	                        }
	                    },
	                    {field:'price',title:'总金额',width:100,
	                         formatter:function(value,row,index){
	                            return "￥"+row.price;
	                        }
	                    },
	                    {field:'memo',title:'备注',width:180},
	                    {field:'opt',title:'操作',width:100,formatter:function (value,row,index) {
	                         if(row.sellPrice==null){
	                    		return null;
	                    	 }
	                            var context="<a style='opacity: 0.2' href=\"javascript:;\">删除</a>&nbsp;&nbsp;" 
	                            return context;
                        }}

	                ]],
	                 onClickRow:function(index,data){
	                  $(this).datagrid('unselectRow',index);
	                 },
	                   onLoadSuccess:function(){
	                	$("#partList").datagrid('appendRow',{
	                	  'partBrand':"合计",
	                	  'amount':total('amount'),
	                	  'price':total('price')
	                    });
	                   }
	                  });
	                }
            });
         $("#search_btn").click(function(){
	     	var form=$("#form1");
  		 	$("#productList").datagrid('load',serializeForm(form));
         });
         
          $("#reset_btn").click(function(){
	        	$("#form1").form('reset');
             	$("#productList").datagrid('load',{});
          });
        });
        
        function total(col){
        	var rows=$("#partList").datagrid('getRows');
        	var total=0;
        	for(var i=0;i<rows.length;i++){
        		total+=parseFloat(rows[i][col]);
        	}
        	return total;
        }
        
       	//将表单序列化
		function serializeForm(form){
			var obj={};
			$.each(form.serializeArray(),function(index){
				  if(obj[this['name']]){
					obj[this['name']]=obj[this['name']]+','+this['value']
				 }else{
				   obj[this['name']]=this['value'];
				 }
			});
			return obj;
		}  
		
        function detail(id) {
            $.messager.alert("信息","查看"+id+"产品详情");
        }
        function addSalesOrders() {
            parent.addTabs("销售订单","goods/addSalesOrder.jsp");
        }
        function showSalesOrders(code) {
            parent.addTabs("销售订单","/kxmanage/sellorderbill/showsellorderoillbycode?code="+code);
        }
        function updateSalesOrders(code) {
            parent.addTabs("销售订单","/kxmanage/sellorderbill/updatesellorderoillbycode?code="+code);
        }
        
        function delSalesOrders(code,checkStatus){
         $.messager.confirm('确认','您确认想要删除记录吗？',function(r){
		   var ids=code;
           if (r==true){
             if(checkStatus==2){
                $.messager.alert('消息',"选择的销售单中有已审核的，请重新选择！");
             }else{
              $.ajax({
		      type:"post",//请求类型
		      url:"/kxmanage/sellorderbill/delsellorderbill?code="+code, 
		      dataType:"json",//ajax接口（请求url）返回的数据类型
		      success:function(data){//data：返回数据（json对象）
		         if(data.result=="success"){
		              $.messager.alert('消息',ids+" 删除成功！");
                      $('#productList').datagrid('reload');//删除后重新加载下
                      var oldCode=$("#oldCode").html();
                      if(code==oldCode){
                        $("#show").html("<div id='detailed' style='margin-bottom:10px'></div><div id='partList' class='easyui-datagrid'></div>");
                      }
		         }else if(data.result=="error"){
		              $.messager.alert('消息',ids+" 删除失败！");
		         }
		          else if(data.result=="noPower"){
        			alert("您没有权限！");
        		 } 
		      }
		      }); 
		     }
           }
         })
        }
        function del() {
            var rows=$("#productList").datagrid('getSelections');
            var flag=true;
            var codes=[];
            for(var i=0;i<rows.length;i++){
              if(rows[i].checkStatus==2){
                flag=false;
                break;
              }
               codes.push(rows[i].code);
            }
            var jsoncodes=JSON.stringify(codes);
            if(rows.length==0){
                $.messager.alert('消息',"请选择记录");
                return;
            }
            $.messager.confirm('确认','您确认想要删除记录吗？',function(r){
                if (r==true){
                     if(!flag){
                        $.messager.alert('消息',"选择的销售单中有已审核的，请重新选择！");
                     }else{
                      $.ajax({
		                type:"post",//请求类型
		                url:"/kxmanage/sellorderbill/delallsellorderbill?codes="+jsoncodes, 
		                dataType:"json",//ajax接口（请求url）返回的数据类型
		                success:function(data){//data：返回数据（json对象）
		                  if(data.result=="success"){
		                     $.messager.alert('消息',"删除成功");
                             $('#productList').datagrid('reload');//删除后重新加载下
                             var oldCode=$("#oldCode").html();
                             for(var i=0;i<codes.length;i++){
                                if(codes[i]==oldCode){
                                  $("#show").html("<div id='detailed' style='margin-bottom:10px'></div><div id='partList' class='easyui-datagrid'></div>");
                                }
                             }
		                  }else if(data.result=="error"){
		                     $.messager.alert('消息',"删除失败！");
		                  }
		                }
		             }); 
		            }
                }
            });
        }
       
       function reload(){
         window.location.reload();
       }
       
       function execl(){
            $.messager.confirm('确认','您确认想要导出excel文件吗？（下载路径为浏览器默认下载路径）',function(r) {
                if (r) {
                    var findcode=$("#findcode").val();
                    var findstartDate=$("#findstartDate").val();
                    var findendDate=$("#findendDate").val();
                    var findsupplierName=$("#findsupplierName").val();
                    location.href="/kxmanage/sellorderbill/getExcel?findcode="+findcode+"&findstartDate="+findstartDate
						+"&findendDate="+findendDate+"&findsupplierName="+findsupplierName;
                }
            });
       }
    </script>

</head>
<body>
<div id="wu_toolbar">
   
    <div class="wu_toolbar_button">
        <a  href="#" onclick="hideTable()" class="easyui-linkbutton" data-options="iconCls:'icon-search'" plain="true">查询</a>|
        <a  href="#" onclick="addSalesOrders()" class="easyui-linkbutton" data-options="iconCls:'icon-add'" plain="true">添加</a>|
        <a  href="#" onclick="del()" class="easyui-linkbutton" data-options="iconCls:'icon-delete-self'" plain="true">删除</a>|
        <a  href="#" onclick="reload()" class="easyui-linkbutton" data-options="iconCls:'icon-page-refresh'" plain="true">刷新</a>|
        <a  href="#" onclick="execl()" class="easyui-linkbutton" data-options="iconCls:'icon-communication'" plain="true">导出excal</a>
    </div>
    <div id="searchdiv" style="display: block;width: 100%;">
        <form name="form1" id="form1"> 
            <input type="hidden" id="page" name="page" value="1"/>
            <input type="hidden" id="rows"  name="rows" value="2"/>
            <input type="hidden" id="path" value="${pageContext.request.contextPath}"/>
            <table width="100%" border="0" cellspacing="0" style="background-color:#c9dfff;" >
                <tbody><tr>
                    <td width="70"><b>检索条件:</b></td>
                    <td width="70" align="right">询价编号:</td>
                    <td width="150" align="left"><input name="findcode" class="easyui-textbox" type="text" id="findcode" value="" maxlength="100"></td>
                    <td width="70" align="right">开始日期:</td>
                    <td width="150" align="left"><div class="l-text-wrapper"><div class="l-text l-text-date"><input name="findstartDate" type="text" id="findstartDate" value="" maxlength="30" class="easyui-datebox" ligeruiid="findstartDate"><div class="l-text-l"></div><div class="l-text-r"></div><div class="l-trigger"><div class="l-trigger-icon"></div></div></div></div></td>
                    <td width="70" align="right">结束日期:</td>
                    <td width="150" align="left"><div class="l-text-wrapper"><div class="l-text l-text-date"><input name="findendDate" type="text" id="findendDate" value="" maxlength="30" class="easyui-datebox" ligeruiid="findendDate"><div class="l-text-l"></div><div class="l-text-r"></div><div class="l-trigger"><div class="l-trigger-icon"></div></div></div></div></td>
                    <td width="70" align="right">供应商名:</td>
                    <td width="150" align="left"><input name="findsupplierName" class="easyui-textbox" type="text" id="findsupplierName" value="" maxlength="100"></td>
                    <td width="72">
                        <a id="search_btn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'">搜索</a>
                    </td>
                    <td width="72">
                        <a id="reset_btn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-refresh'">重置</a>
                    </td>
                    <td width="0"></td>
                </tr>
                </tbody>
            </table>
            <input type="hidden" id="menuCode" value="100001001">
        </form>
    </div>
</div>
<div id="productList" class="easyui-datagrid">

</div>
<div id="show" style="margin-top:10px;display:none">
  <div id="detailed" style="margin-bottom:10px">
    
  </div>
  <div id="partList" class="easyui-datagrid">
     
  </div>
</div>
</body>
</html>